import { StoreFuncs } from "../../ExcelStore";

export interface IClearContentOptions {
    sheetName: string;
    oldDatas: any;
}
export default {
    canUndo: true,
    execute: (context: GC.Spread.Sheets.Workbook, options: IClearContentOptions, isUndo: boolean) => {
        const { sheetName } = options;
        const sheet = context.getSheetFromName(sheetName);
        const selections = sheet.getSelections();
        const store = StoreFuncs.getInstance();
        const needSuspernded = !sheet.isPaintSuspended()
        if (needSuspernded) {
            sheet.suspendPaint();
        }
        if (store.editable && store.table && store.dataSource) {
            const datas: any[] = store.dataSource.getValue('datas') as any[];
            if (isUndo) {
                const { row: startRow } = store.table.dataRange();
                selections.forEach(selection => {
                    const { row, col, rowCount, colCount } = selection;
                    for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
                        const currentRow = row + rowIndex;
                        const dataIndex = currentRow - startRow;
                        const data = datas[dataIndex];
                        for (let colIndex = 0; colIndex < colCount; colIndex++) {
                            const currentCol = col + colIndex;
                            const column = store.backColumn[currentCol];
                            if (column.editable) {
                                const { value, valueSpan } = options.oldDatas[`${currentRow}.${currentCol}`];
                                sheet.setValue(currentRow, currentCol, valueSpan);
                                StoreFuncs.changeValue({
                                    sheet,
                                    row: currentRow,
                                    col: currentCol,
                                    newValue: { value },
                                    oldValue: { value: data[column.column] },
                                    sheetName,
                                    time:1
                                });
                            }
                        }
                    }
                });
            } else {
                const { row: startRow } = store.table.dataRange();
                options.oldDatas = {};
                selections.forEach(selection => {
                    const { row, col, rowCount, colCount } = selection;
                    for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
                        const currentRow = row + rowIndex;
                        const dataIndex = currentRow - startRow;
                        const data = datas[dataIndex];
                        for (let colIndex = 0; colIndex < colCount; colIndex++) {
                            const currentCol = col + colIndex;
                            const column = store.backColumn[currentCol];
                            if (column.editable&&column.column!="id") {
                                options.oldDatas[`${currentRow}.${currentCol}`] = {
                                    value: data[column.column],
                                    valueSpan: data[`${column.column}span`]
                                };
                                sheet.setValue(currentRow, currentCol, '');
                                StoreFuncs.changeValue({
                                    sheet,
                                    row: currentRow,
                                    col: currentCol,
                                    newValue: { value:"" },
                                    oldValue: { value: data[column.column] },
                                    sheetName,
                                    time:1
                                });
                            }
                        }
                    }
                });
            }
        }
        if (needSuspernded) {
            sheet.resumePaint();
        }
    }
};